package com.bidevalution.support.util;

import com.qtp.core.util.OConvertUtils;
import org.apache.log4j.Logger;
import org.dom4j.*;
import org.dom4j.io.SAXReader;
import sun.misc.BASE64Decoder;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;

/**
 * @description: 标书操作工具类
 * @author: xiayf
 * @date: 2019-08-20 17:46
 */
public class DecryptUtil {

    private static Logger log = Logger.getLogger(DecryptUtil.class);

    public static void main(String[] args) throws Exception {
//        bidFileDecrypt();
        String src = "E:\\(1234567890987654321)杭州监理测试003.网络加密标书.加密标书.zip";
        String dest = "E:\\";
        String pass = "sCBshNvoMgfgxwSZ7qm4fbWKJSoAGGKKaoY8Plh2VGLlwhN1ZwD0hOlJFOYm6rsOeP0TXvgdBrd3dGdSqaSaHgiwVQLSPFZ0p745ok7A8VnGQvPw3gtunynbv80b34Rn";
        ZipUtil.unZip(src,dest,pass);
    }

    /**
     * 标书解密
     *
     * @throws Exception
     */
    public static void bidFileDecrypt() throws Exception {
        SAXReader saxReader = new SAXReader();
        Document doc = null;
        try {
            doc = saxReader.read(new File("E:\\(1234567890987654321)杭州监理测试003.网络加密标书"));
        } catch (DocumentException e) {
            log.error(e.getMessage(), e);
            throw e;
        }
        Element root = doc.getRootElement();
        Map<String, Object> map = new HashMap<>();
        List<String> list = new ArrayList<>();
        getXmlInfo(root, map, list);
        int bidVersion = OConvertUtils.getInt(map.get("ConstructProject-BidVersion"));
        if(bidVersion < 4){
            throw new Exception("标书版本不是最新版！");
        }
        writeBidFile(list);
        System.out.println(map.get("ConstructProject-UniqueCode"));
    }

    /**
     * 生成标书
     *
     * @param list
     */
    private static void writeBidFile(List<String> list) throws Exception {
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
            String value = list.get(i);
            byte[] bytes;
            FileOutputStream fos = null;
            try {
                bytes = new BASE64Decoder().decodeBuffer(value);
                if (i == 0) {
                    fos = new FileOutputStream("E:/(1234567890987654321)杭州监理测试003.网络加密标书.加密标书.zip");
                    fos.write(bytes);
                } else {
                    fos = new FileOutputStream("E:/(1234567890987654321)杭州监理测试003.网络加密标书.加密标书.zip", true);
                    fos.write(bytes);
                }
                fos.flush();
            } catch (IOException e) {
                log.error(e.getMessage(), e);
                throw e;
            } finally {
                try {
                    if (fos != null) {
                        fos.close();
                    }
                } catch (IOException e) {
                    log.error(e.getMessage(), e);
                }
            }
        }
    }

    /**
     * 获取xml格式信息
     *
     * @param element
     * @param map
     * @param list
     */
    private static void getXmlInfo(Element element, Map<String, Object> map, List<String> list) {
        // 获取到属性迭代器添加所有的属性
        Iterator<Attribute> attriIt = element.attributeIterator();
        while (attriIt.hasNext()) {
            Attribute attribute = attriIt.next();
            if (element.getName().indexOf("BidFile") >= 0) {
                list.add(attribute.getValue());
            } else {
                map.put(element.getName() + "-" + attribute.getName(), attribute.getValue());
            }
        }
        // 获取所有的子节点
        Iterator<Node> it = element.nodeIterator();
        while (it.hasNext()) {
            Node node = it.next();
            if (node instanceof Text) {
                // 文本
                System.out.println(node.getText());
            } else if (node instanceof Comment) {
                // 注释
                System.out.println("<!--" + node.getText() + "-->");
            } else if (node instanceof Element) {
                // 迭代读取下一个元素
                getXmlInfo((Element) node, map, list);
            }
        }
    }


}
